﻿using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceWarrantyDealWith;

namespace ZBJF.Warranty.BLL
{
    public partial class StatusReasonService
    {
        #region 根据主键ID获取状态原因 + ServiceProvider LoadByID(int id)
        public StatusReason LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断状态原因是否存在+bool IsExist(string accessoriestName, int id = 0)
        /// <summary>
        /// 判断状态原因是否存在
        /// </summary>
        /// <param name="accessoriestName">状态原因名称</param>
        /// <param name="id">状态原因ID</param>
        /// <returns></returns>

        public bool IsExist(string reasonDescribe, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<StatusReason>();
            if (!string.IsNullOrWhiteSpace(reasonDescribe))
                expressionWhere = expressionWhere.And(o => o.ReasonDescribe.Equals(reasonDescribe));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页获取状态原因列表 + IQueryable<ServiceProvider> GetPagingList(ServiceProviderModel model, out int total)
        /// <summary>
        /// 分页获取状态原因列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public IQueryable<StatusReason> GetPagingList(StatusReasonModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<StatusReason>();

            //工单状态ID
            if (model.InsuranceWarrantyRecordStatusId > 0)
            {
              var childrenStatusIds =  ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.LoadEntities(t =>t.ID== model.InsuranceWarrantyRecordStatusId || t.ParentId == model.InsuranceWarrantyRecordStatusId).AsNoTracking().Select(t=>new
                {
                    t.ID
                }).ToArray();
                int[] temp = new int[childrenStatusIds.Length];
                for (int i=0,j=childrenStatusIds.Length; i<j; i++)
                {
                    temp[i] = childrenStatusIds[i].ID;
                }
                expressionWhere = expressionWhere.And(o => temp.Contains( o.InsuranceWarrantyRecordStatusId ));
            }

            //状态原因名称
            if (!string.IsNullOrEmpty(model.ReasonDescribe))
            {
                string kw = model.ReasonDescribe;
                expressionWhere = expressionWhere.And(o => o.ReasonDescribe.Contains(kw));
            }

            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }
            return LoadPagerEntities<StatusReason>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion
    }
}
